import React from "react";
import { Modal, Form, Button, Card, Checkbox} from 'antd';
import {validateListApi, modifyFunArgApi} from "../api/projectConfig";
import Qs from 'qs'
import {withRouter} from "react-router-dom";
import ValidateUnit from "./ValidateUnit";


class ValidatePanel extends React.Component {

    argVo;
    Valid_Checked_Dic = {}; // 选中的验证内容
    cleanCallBack = {}

    state = {
        visible: false,
        validates:[]
    }

    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.onRef(this)
    }

    handleOk = () => {

        var arr = [];
        for(var k in this.Valid_Checked_Dic) {
            arr.push(this.Valid_Checked_Dic[k]);
        }

        // to update
        let _valids = JSON.stringify(arr);

        let restRet = modifyFunArgApi({id:this.argVo.id, valid: _valids})
        restRet.then(ret => {
            this.handleCancel()
        })

    }

    handleCancel = () => {
        this.props.hideValidatePanel()
        this.hideValidatePanel()
    }

    hideValidatePanel = () => {
        this.setState({ ...this.state, visible: false });
    }

    showPanel = (argVo)=>{
        if(argVo) {
            this.argVo = argVo
        }

        this.setState({
            ...this.state,
            visible: true
        })

        let {location: {search}} = this.props,
            {projectId = 0} = Qs.parse(search.substr(1)) || {};

        let validateData = validateListApi(projectId)
        validateData.then(res=>{
            let args = res.data.data
            this.setState({ ...this.state, validates: args });

        })


        // 首次为空所以不调用 *********
        this.Valid_Checked_Dic = {};

        for(var kk in this.cleanCallBack) {
            let cb = this.cleanCallBack[kk];
            cb.call(window);
        }
    }

    render() {

        let {visible, validates} = this.state;
        let _valid;
        if(this.argVo) {
            _valid = this.argVo.valid;
        }


        var checkedValidDic = {} // 已选中验证

        if(_valid) {
            let arr = eval(_valid);
            for(var i=0; i<arr.length; i++) {
                let _val = arr[i];
                let _key = _val.substring(0, _val.indexOf('('))
                var _obj = { checked: true }
                var _vals = _val.substring(_val.indexOf('\(') + 1, _val.lastIndexOf('\)'));
                if (_vals) { // 有可能为空字符串
                    let vvArr = _vals.split(',');
                    for (var j = 0; j < vvArr.length; j++) {
                        let key_val = vvArr[j].split('=');
                        _obj[key_val[0]] = key_val[1]
                    }
                }

                checkedValidDic[_key] = _obj
                this.Valid_Checked_Dic[_key] = _val
            }
        }

        return (
            <Modal
                width={800}
                visible={visible}
                keyboard={false}
                maskClosable={false}
                title="params"
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                footer={[
                    <Button key="ok" className="ant-btn ant-btn-primary" onClick={this.handleOk}>保存</Button>,
                    <Button key="back" onClick={this.handleCancel}>关闭</Button>
                ]}
            >
                <Form className="form_item_4">

                    <Form.Item label="数据格式验证" >
                        <Card>
                            {validates.map((value, index) =>{

                                let _initValue = checkedValidDic[value.name];

                                return <ValidateUnit key={'ValidateUnit_' + index} cleanChild={(cb)=>{
                                    if( ! this.cleanCallBack[value.name]) {
                                        cb.call(window)
                                    }
                                    this.cleanCallBack[value.name] = cb;
                                }} initValue={_initValue} key={'v_u_k_'+index} onchangeVal={(atype, val) => {
                                    console.info('atype=' + atype + ',val=' + val)
                                    if(atype == 'add') {
                                        this.Valid_Checked_Dic[value.name] = val;
                                    } else {
                                        delete this.Valid_Checked_Dic[value.name];
                                    }
                                }} objectIndex={index} validateModel={value}></ValidateUnit>
                            })}
                        </Card>

                    </Form.Item>

                    <Form.Item label="业务数据验证" >
                        <Card>

                        </Card>
                    </Form.Item>

                </Form>

            </Modal>
        )
    }
}

const _ValidatePanel = withRouter(Form.create()(ValidatePanel));
export default _ValidatePanel